<?php 
    class PaymentAction extends Action{
        public function index(){
            
        }
        
        //加载地址
        public function loadAddress(){
             $uId=1;

             $data=M('user_info')->where("UserId='$uId'")->select();
             if(count($data)!=0){
                 $this->assign("AddressList",$data)->display('Payment');
             }else{
                 $this->display('Payment');  
             }
             
        }
        
        //传递相关信息
        public function getInfo(){
            //获取参数
            $UserId=$_POST['isUserId'];
            $OrderId=$_POST['isOrderId'];
            
            $money=M('user')->where("UserId='$UserId'")->select()[0]['Money'];
            $OrderTotal=M('order')->where("OrderId='$OrderId'")->select()[0]['Total'];
            $data=array(
                'Money'=>$money,
                'OrderTotal'=>$OrderTotal
            );
            
            $this->ajaxReturn($data,'json');
        }
        
        //验证密码并支付
        public function checkPasswd(){
            //获取参数
            $UserId=$_POST['isUserId'];
            $Passwd=$_POST['isPasswd'];
            $OrderId=$_POST['isOrderId'];
            $AddressId=$_POST['AddressId'];
            
            $Order=M('order')->where("OrderId='$OrderId'")->select()[0];
            
            if($Order['OrderState']!=1){//待付款订单不存在
                $this->ajaxReturn(4);
            }
            
            
            modifyOrder($OrderId,$AddressId);//修改收货地址
            
            $money=M('user')->where("UserId='$UserId'")->select()[0]['Money'];
            $temp=M('user')->where("Userid='$UserId' and PayPassword='$Passwd'")->select();
            
            if(count($temp)>0){
                
                if($money<$Order['Total']){//余额不足
                    $this->ajaxReturn(3);
                }
                
               
                //扣除费用,更改订单状态
                $data1['Money']=$money-$Order['Total'];
                M('user')->where("UserId='$UserId'")->save($data1);
                $data2['OrderState']=2;
                $data2['payDate']=time();
                M('order')->where("OrderId='$OrderId'")->save($data2);
                
                
                //更新商品销售信息
                $os=getOrderItem($OrderId);
                //p($os);
                foreach ($os as $key => $value) {
                    updateProInfo($value['ProId'],$value['ProCount'],2);
                }
                
                //更新用户信息
                $data3['UserId']=$UserId;
                $u=M('user')->where("UserId='$UserId'")->select()[0];
                $data3['TotalSpend']=doubleval($u['TotalSpend'])+doubleval($Order['Total']);
                $data3['TotalCount']=intval($u['TotalCount'])+getOrderProCount($OrderId);
               // p($data3);
                M('user')->save($data3);
                
            }else{//密码错误
               $this->ajaxReturn(2);
            }
            //echo 'aaaaaaaaa';
            $this->ajaxReturn(1);
        }
        
        
        
        
    }
?>